<template>
  <div id="login">
    <!-- <h1>登录</h1> -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录
        </van-button>
        <br />
        <van-button round block type="primary" @click="register"
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>
const qs = require("qs");

export default {
  name: "Login",
  data() {
    return {
      username: "test",
      password: "123456",
      captcha: "",
    };
  },
  methods: {
    onSubmit(values) {
      this.$api.axios
        .post(
          `${this.$api.baseURL.id4}connect/token`,
          qs.stringify({
            client_id: "JiZhangSoft_App",
            client_secret: "1q2w3e*",
            grant_type: "password",
            username: values.username,
            password: values.password,
          })
        )
        .then((r) => {
          // console.log(r.data);
          localStorage.setItem("access_token", "Bearer " + r.data.access_token);
          this.$router.push("/zhangdan");
        });
    },
    register() {
      location.href = `${this.$api.baseURL["id4"]}Account/Register?returnUrl=${this.$api.baseURL["client"]}dist/index.html#/login`;
    },
  },
};
</script>
<style lang="stylus" scoped></style>
